<?php

function list_kalendar_periode(){
	$periode_id=get_periode_absen();
	$periodes = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periodes['tanggal']));
	$tahun = $periodes['tahun'] ;
	$periode_absen=sprintf( '%02d',$bulan).'-'.$tahun ;
	my_set_code_js('
		function confirm_proses(){
			var t = confirm(\'Create schedule periode '.$periode_absen.'?\');
			if(t){
				location.href=\'index.php?com=a_tutup_bulan&task=create_schedule\';
			}
			return false;
		}
	');

	$header = array(
		'Tahun'=>array('style'=>'border-bottom:2px solid;width:50%'),  
		'Bulan'=>array('style'=>'text-align:left;border-bottom:2px solid;width:40%'),   
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	$query = "SELECT * FROM pr_periode 
		WHERE periode_id<={$periode_id}
		ORDER BY periode_id DESC";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while( $ey =my_fetch_array($result) ){
		$detailproperty = array(
				'href'=>'index.php?com='.$_GET['com'].'&task=detail&periode_id='.$ey['periode_id'],
				'title'=>'Detail periode'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		$row[] = array(
			'tahun'=>$ey['tahun'],
			'bulan'=>date('m',strtotime($ey['tanggal'])),
			' v'=>position_text_align($detail_button,'right'),
		);
	}
	$datas = table_rows($row);
	$menuontop = array(  
		'Create Schedule'=>array('onclick'=>'javascript:confirm_proses();'),
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	$paging = $kgPagerOBJ ->showPaging();
	return /*$printmenu . */table_builder($header , $datas ,  3 , false , $paging);
	
}
function form_generate_kalender_shift(){
	$view = form_header( "calon karyawan" , "cp"  );
	$query = "
		SELECT tahun, MONTH( tanggal ) AS bulan_periode
		FROM pr_periode
		ORDER BY tahun DESC , MONTH( tanggal ) DESC LIMIT 0,1 ";
	$res = my_query($query);
	$row = my_fetch_array($res);  
	$tahun = ($row['bulan_periode'] > 11 ) ? ( $row['tahun'] + 1) : $row['tahun'];
	$bulan = ($row['bulan_periode'] > 11 ) ? 1 : $row['bulan_periode'] + 1;
	$datas = array(
		'tahun' =>my_type_data_str($tahun),	
		'tanggal'=> my_type_data_str($tahun.'-'.$bulan.'-01')
	);
	$periode_id = my_insert_record('pr_periode',$datas); 
	kalendar_shift_proses_update($periode_id);
}

function get_last_date($month , $year , $periode_id,$kelompok_shift_id){
	
	 
	for($n=31;$n>=1 ; $n--){
		$t= urutan_kalendar_usaha($n);
		if($t > 28){
			$d = checkdate  (    $month  ,   $t  ,    $year  ); 
			if(!$d ){
				 continue;
			}
		} 
		
		$shift = get_shift_by_ndate( sprintf('%02d',$t) , $periode_id,$kelompok_shift_id);
		if( is_null($shift) ){
			continue;
		}else{
			return  $n  ;
		}
	}
}

function list_kelompok_shift($periode_id){
	//PASANG JAVASCRIPT
	my_set_code_js('

		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com=kalendar_shift&task=delete&periode_id='.$periode_id.'&id=\'+id;
			}
			return false;
		}

	');
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ;
	if($bulan =='1' ){
		$startdate = ($tahun -1).'-12-11';
		$middledata = ($tahun -1).'-12-25';
		$middledata2 = ($tahun -1).'-12-26';
		$enddate = $tahun.'-'.sprintf( '%02d',$bulan) .'-10';
	}else{
		$startdate = $tahun.'-'.sprintf( '%02d',$bulan-1) .'-11';
		$middledata =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-25';
		$middledata2 =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-26';
		$enddate =$tahun.'-'.$bulan .'-10';
	}
	$tanggals = list_kalender($startdate , $middledata  );
	$tanggals = array_unique($tanggals) ;
	
	$header = array(
		'Tipe Waktu Kerja'=>array('style'=>'border-bottom:2px solid;width:17%'),  
		'Kelompok'=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),   
		'Kode Pola'=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),   
		'Pola'=>array('style'=>'text-align:left;border-bottom:2px solid;width:53%'),   
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	
	$query = "SELECT b.label,a.kelompok_shift_kode, c.label as label_pola, c.pola, a.kelompok_shift_id,a.pola_id FROM a_kelompok_shift a
		INNER JOIN a_waktu_kerja b ON a.waktu_kerja_id = b.waktu_kerja_id
		LEFT JOIN a_pola_shift c ON a.pola_id=c.pola_shift_id 
		WHERE   jenis='Shift' ";
	$result = my_query($query);
	$row = array();
	while( $ey =my_fetch_array($result) ){
 
		$detail  = array(
					'href'=>'index.php?com='.$_GET['com'].'&task=detail_kelompok&periode_id='.$_GET['periode_id'].'&kelompok_shift_id='.$ey['kelompok_shift_id'],
					'title'=>'Setting Jadwal Kerja',
					'rel'=>'facebox',
				);
		$detail_button = button_icon( 'b_props.png' , $detail   ); 
		
		if($ey['pola_id']>0){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{
			$deleteproperty = array(
				'href'=>'javascript:; ',
				'onclick'=>'javascript:confirmDelete('.$ey['kelompok_shift_id'].');',
				'title'=>'Delete'
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}
		
		$row[] = array(
			'waktu_kerja'=>$ey['label'],
			'kelompok'=>$ey['kelompok_shift_kode'],
			'label_pola'=>$ey['label_pola'],
			'pola'=>$ey['pola'],
			' v'=>position_text_align($detail_button.' '.$delete_button,'right'),
		);
	}
	$datas = table_rows($row);
	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "Kelompok Shift" , "Kelompok Shift"  );
	$view .= form_field_display( '<span class="label_form">'. date( 'd-m-Y' , strtotime($periode['dari']) ) .' &nbsp;  s/d &nbsp; '. date( 'd-m-Y' , strtotime($periode['sampai']) )  .'</span>'   , "<b>Periode</b>"    ); 
	$view .= '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value="Tambah" class="main_button" onclick="javascript:jQuery.facebox({ ajax: \'index.php?com=kalendar_shift&task=create_group&periode_id='.$periode_id.'\' })"/>
	<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=kalendar_shift\'"/>
	</div></td></tr>'   ; 
	
	return  $view. table_builder($header , $datas ,  3 , false );	
}

function detail_kelompok_shift($periode_id ,$kelompok_shift_id){ 
	my_set_code_js('
	function submitChange(tanggal ,value){
		location.href=\'index.php?com='.$_GET['com'].'&value=\'+value+\'&tanggal=\'+tanggal+\'&task=update&periode_id='.$periode_id .'&kelompok_shift_id='.$kelompok_shift_id.'\'  ;
			
	}
	');
	$kelompok = my_get_data_by_id('a_kelompok_shift','kelompok_shift_id',$kelompok_shift_id);
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ;
	if($bulan =='1' ){
		$startdate = ($tahun -1).'-12-11';
		$middledata = ($tahun -1).'-12-25';
		$middledata2 = ($tahun -1).'-12-26';
		$enddate = $tahun.'-'.sprintf( '%02d',$bulan) .'-10';
	}else{
		$startdate = $tahun.'-'.sprintf( '%02d',$bulan-1) .'-11';
		$middledata =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-25';
		$middledata2 =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-26';
		$enddate =$tahun.'-'.$bulan .'-10';
	}
	$tanggals = list_kalender($startdate , $middledata  );
	$tanggals = array_unique($tanggals) ;
	
	$header = array(
		'Tanggal'=>array('style'=>'text-align:center;border-bottom:2px solid;width:50%'),  
		'Shift'=>array('style'=>'text-align:left;border-bottom:2px solid;width:40%'),   
	);
	$row= array();
	foreach($tanggals as $tanggal){
		$row[] = array(
			'tanggal'=>date('d-m-Y',strtotime($tanggal)),
			'shift'=>get_kelompok_shift($tanggal , $periode_id , $kelompok_shift_id, $kelompok['pola_id'] ),
		);
	}
	$datas = table_rows($row);
	$table1 = table_builder($header , $datas ,  2 , false);
	$tanggals = list_kalender($middledata2 ,$enddate  );
	$tanggals = array_unique($tanggals) ;
	
	$header = array(
		'Tanggal'=>array('style'=>'text-align:center;border-bottom:2px solid;width:50%'),  
		'Shift'=>array('style'=>'text-align:left;border-bottom:2px solid;width:40%'),   
	);
	$row= array();
	foreach($tanggals as $tanggal){
		$row[] = array(
			'tanggal'=>date('d-m-Y',strtotime($tanggal)),
			'shift'=>get_kelompok_shift($tanggal , $periode_id , $kelompok_shift_id, $kelompok['pola_id'] ),
		);
	}
	$datas = table_rows($row);
	$table2 = table_builder($header , $datas ,  2 , false);
	$view ='<table width="98%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .=  form_header( "detail pegawai" , "cp"  );
/*	$view .= '<tr><td><td><div style="width:100%;text-align:right;"> 
	<input type="button" value="BACK" onclick="javascript:location.href=\'index.php?com='.$_GET['com'].'&task=detail&periode_id='.$periode_id.'\'" class="main_button" /> 
 </div></td></tr>'   ; */
	$view .= form_field_display( '<span class="label_form">'. sprintf( '%02d',$bulan).'-'.$tahun .'</span>'   , "<b>Periode</b>"    ); 
	$waktu_kerja = my_get_data_by_id('a_waktu_kerja' ,'waktu_kerja_id' ,($kelompok['waktu_kerja_id']<>'' ? $kelompok['waktu_kerja_id'] : 0 ));
	$view .= form_field_display( '<span class="label_form">'. strtoupper( $waktu_kerja['label']) .'</span>'   , "<b>Tipe Waktu Kerja</b>"    ); 

	$view .= form_field_display( '<span class="label_form">'. strtoupper( $kelompok['kelompok_shift_kode']) .'</span>'   , "<b>Kelompok</b>"    ); 
	$view .= form_footer( );
	$view .= '</td></tr></table><br/>';
	$split ='<div style="height:400px;"><div style="width:49%;float:left">'.$table1.'</div>';
	$split .='<div style="width:49%;float:right">'.$table2.'</div></div>';
	return /*$view .*/ $split;
}

/*
function get_kelompok_shift_value($tanggal , $periode_id , $kelompok_shift_id ){
	list($yyyy,$mm,$dd) = explode("-" , $tanggal);
	 
	$query = "SELECT tgl_{$dd} FROM a_kalendar_shift 
	WHERE periode_id = {$periode_id} AND kelompok_shift_id={$kelompok_shift_id}";
	$result = my_query($query);
	if(my_num_rows($result) > 0){
		$row = my_fetch_array($result);
		$value = $row['tgl_'.$dd];
		if($value <> ''){
			return $value;
		}
		return false;
	}
	return false;
}

*/
function get_kelompok_shift($tanggal , $periode_id , $kelompok_shift_id, $pola_id ){
	list($yyyy,$mm,$dd) = explode("-" , $tanggal);
	$query = "SELECT tgl_{$dd} FROM a_kalendar_shift 
		WHERE periode_id = {$periode_id} AND kelompok_shift_id={$kelompok_shift_id}";
	$result = my_query($query);
	$row = my_fetch_array($result);
	$value = $row['tgl_'.$dd];
	if($pola_id>0){
		return $value ;	
	}else{
		$shifts = array(
			'1'=>'1',
			'2'=>'2',
			'3'=>'3',
			'X'=>'X',
			'N'=>'N',
		);
		
		$shift = array(
			'name'=>'shift',
			'id'=>'shift',
			'onchange'=>'submitChange(\''.$tanggal.'\' ,this.value )',
			'value'=>$value ,
		);
		return form_dropdown($shift,$shifts);
	}	
}

function update_tanggal_shift($tanggal , $periode_id , $kelompok_shift_id , $value){
	$dd = date('d',strtotime($tanggal));
	$kalendar_shift_id =get_data_kalendar_shift_by_kelompok_shift_id_and_periode_id($periode_id , $kelompok_shift_id);
	if(! $kalendar_shift_id){
		$query = "INSERT INTO a_kalendar_shift SET
		periode_id ={$periode_id},
		kelompok_shift_id={$kelompok_shift_id},
		tgl_{$dd} ='{$value}'
		";
		return my_query($query);
	}
	 
	$query ="UPDATE a_kalendar_shift SET tgl_{$dd} ='{$value}' WHERE kalendar_shift_id ={$kalendar_shift_id} ";
	return my_query($query);
	 
}
 
function get_data_kalendar_shift_by_kelompok_shift_id_and_periode_id($periode_id , $kelompok_shift_id){
	list($yyyy,$mm,$dd) = explode("-" , $tanggal);
	$query = "SELECT * FROM a_kalendar_shift 
	WHERE periode_id = {$periode_id} AND kelompok_shift_id={$kelompok_shift_id}";
	$result = my_query($query); 
	if(my_num_rows($result) > 0){
		$row = my_fetch_array($result);
		return $row['kalendar_shift_id'];
	}
	return false;
}


function form_create_group($periode_id){
	$view = form_header( "kelompok" , "kelompok"  );
	$kelompok = array(
				'name'=>'kelompok',
				'value'=>( isset($_POST['kelompok']) ? $_POST['kelompok'] : ' ' ),
				'id'=>'kelompok',
				'type'=>'text',
				'size'=>'10'
			);
	$form_field = form_dynamic($kelompok);	
	$view .= form_field_display( $form_field   , "Kelompok"   );
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function create_group_validate($periode_id){
	$errsubmit = false;
	$err = array();
	 
	/*CHECK CODE*/
	$kelompok =  trim($_POST["kelompok"]); 
	$data_code = my_result("SELECT kelompok_shift_id FROM a_kelompok_shift WHERE kelompok_shift_kode='{$kelompok}' "); 
	$exists_code = ( $data_code == '' ) ? false : true ;  
	if( $kelompok == ''){
		$errsubmit = true;
		$err[] = "Nama Kelompok belum diisi";
	}
	elseif($exists_code){
		$errsubmit = true;
		$err[] = "Nama Kelompok sudah digunakan";	
	}
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function submit_group($periode_id){
	$datas = array(
		'waktu_kerja_id'=>my_type_data_int( 4 ),
		'kelompok_shift_kode'=>my_type_data_str( $_POST['kelompok'] ),
		'pola_id'=>my_type_data_int(0),
	);
	$kelompok_shift_id=my_insert_record('a_kelompok_shift' , $datas);
	$datas = array(
		'kelompok_shift_id'=>my_type_data_int($kelompok_shift_id),
		'periode_id'=>my_type_data_int( $periode_id ),
	);	
	return my_insert_record('a_kalendar_shift' , $datas);
	
}


function remove_group($id){
	$query= "DELETE FROM a_kelompok_shift WHERE kelompok_shift_id = {$id}";
	my_query($query);
	$periode_id=get_periode_absen();	
	$query= "DELETE FROM a_kalendar_shift WHERE kelompok_shift_id = {$id} and periode_id = {$periode_id}";
	return my_query($query);
} 
 